const errorTypes = require('../constans/error-types')

const errorHandle = (error, ctx) => {
    let status, message;

    switch (error.message) {
        case errorTypes.NAME_OR_PASSWORD_IS_REQUIRED:
            status = 400; // Bad Request 错误的请求结果
            message = "用户名或者密码不能为空~";
            break;
        case errorTypes.PARAMSISNULL:
            status = 400; // Bad Request 错误的请求结果
            message = "请求参数不能为空~";
            break;
        case errorTypes.USER_ALREADY_EXISTS:
            status = 409; // conflict 数据库冲突
            message = "用户名已经存在~";
            break;
        case errorTypes.USER_DOES_NOT_EXISTS:
            status = 400; // 参数错误
            message = "用户不存在~";
            break;
        case errorTypes.PASSWORD_IS_INCORRENT:
            status = 400; // 参数错误
            message = "密码是错误的~";
            break;
        case errorTypes.UNAUTHORIZATION:
            status = 401; // 授权过期
            message = "无效的token~";
            break;
        case errorTypes.UNPERMISSION:
            status = 401; // 授权过期
            message = "您不具备操作的权限~";
            break;
        default:
            status = 404;
            message = "NOT FOUND";
    }

    ctx.status = status;
    ctx.body = message;
}
module.exports = errorHandle